/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.Kasse;

import controller.SalgsController;
import java.awt.CardLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import model.Kategori;
import model.Vare;
import utility.ImageToolbox;
import utility.NumberRestrictionDocument;
import utility.PageHandler;
import view.MainGUI;
import view.SkærmNumpad;

/**
 *
 * @author Jeppe
 */
public class KassePanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;
    private PageHandler ph;
    private KategoriPanel kategoriPanel;
    private VarePanel varePanel;
    private final SkærmNumpad skærmNumpad;

    /**
     * Creates new form KassePanel
     */
    public KassePanel(MainGUI mainGUI) {
        initComponents();
        this.mainGUI = mainGUI;
        ph = new PageHandler((CardLayout) kategoriCardLayoutPanel.getLayout(), kategoriCardLayoutPanel);
        setSize(770, 608);
        skærmNumpad = SkærmNumpad.getInstance();
        kategoriPanel = new KategoriPanel(this);
        varePanel = new VarePanel(this);
        ph.addPage(kategoriPanel, KategoriPanel.class.getName());
        ph.addPage(varePanel, VarePanel.class.getName());
        ph.show(KategoriPanel.class.getName());
//        if (isShowing()) {
//            skærmNumpad.setLocation(mainGUI.getX() + jPanel4.getLocationOnScreen().x, mainGUI.getY() + jPanel4.getLocation().y + 90 + 105);
//        }

//        skærmNumpad.setVisible(true);
        kategoriPanel.addCategories();
        revalidate();
        repaint();
    }

    public Rectangle getSkærmNumpadPlacering() {
        return jPanel4.getBounds();
    }

    protected void visKategoriPanel(){
        ph.show(KategoriPanel.class.getName());
    }
    
    protected void showCategory(Kategori kategori) {
        varePanel.visVarerVedKategori(kategori);
        ph.show(VarePanel.class.getName());
    }
    
    protected void tilføjVareTilSalg(Vare vare){
        try {
            SalgsController.getInstance().tilføjVareTilSalg(vare, Integer.parseInt(antalText.getText()));
        } catch (SQLException ex) {
            Logger.getLogger(KassePanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(KassePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        isPindeOgVaflerKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)isPindeOgVaflerKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        mixIsPakkerKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)mixIsPakkerKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        limonadeIsKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)limonadeIsKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        børneIsKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)børneIsKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        specialIsKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)specialIsKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        desserterKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)desserterKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        literIsKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)literIsKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        tooMuchKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)tooMuchKnap.getIcon(), 0, 0, isPindeOgVaflerKnap.getWidth(), isPindeOgVaflerKnap.getHeight());
            }
        };
        kategoriCardLayoutPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        antalText = new javax.swing.JTextField();

        jPanel2.setOpaque(false);
        jPanel2.setLayout(null);

        isPindeOgVaflerKnap.setText("Mix Is-pakker");
        jPanel2.add(isPindeOgVaflerKnap);
        isPindeOgVaflerKnap.setBounds(150, 10, 130, 130);

        mixIsPakkerKnap.setText("Mix Is-pakker");
        jPanel2.add(mixIsPakkerKnap);
        mixIsPakkerKnap.setBounds(10, 10, 130, 130);

        limonadeIsKnap.setText("Mix Is-pakker");
        jPanel2.add(limonadeIsKnap);
        limonadeIsKnap.setBounds(150, 290, 130, 130);

        børneIsKnap.setText("Mix Is-pakker");
        jPanel2.add(børneIsKnap);
        børneIsKnap.setBounds(10, 150, 130, 130);

        specialIsKnap.setText("Mix Is-pakker");
        jPanel2.add(specialIsKnap);
        specialIsKnap.setBounds(150, 150, 130, 130);

        desserterKnap.setText("Mix Is-pakker");
        jPanel2.add(desserterKnap);
        desserterKnap.setBounds(10, 290, 130, 130);

        literIsKnap.setText("Mix Is-pakker");
        jPanel2.add(literIsKnap);
        literIsKnap.setBounds(10, 430, 130, 130);

        tooMuchKnap.setText("Mix Is-pakker");
        jPanel2.add(tooMuchKnap);
        tooMuchKnap.setBounds(150, 430, 130, 130);

        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        kategoriCardLayoutPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        kategoriCardLayoutPanel.setOpaque(false);
        kategoriCardLayoutPanel.setLayout(new java.awt.CardLayout());
        add(kategoriCardLayoutPanel);
        kategoriCardLayoutPanel.setBounds(10, 20, 290, 640);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setOpaque(false);
        add(jPanel3);
        jPanel3.setBounds(310, 20, 300, 340);

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        jPanel4.setOpaque(false);
        jPanel4.setRequestFocusEnabled(false);
        jPanel4.setLayout(null);
        add(jPanel4);
        jPanel4.setBounds(310, 370, 300, 290);

        jButton1.setText("jButton1");
        add(jButton1);
        jButton1.setBounds(640, 20, 180, 80);

        antalText.setDocument(new NumberRestrictionDocument(2));
        antalText.setText("1");
        antalText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                antalTextFocusLost(evt);
            }
        });
        add(antalText);
        antalText.setBounds(650, 180, 100, 30);
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        skærmNumpad.setVisible(true);
//        skærmNumpad.setLocation(mainGUI.getX() + jPanel4.getLocation().x + 10, mainGUI.getY() + jPanel4.getLocation().y + 90 + 30);
//        System.out.println("SkærmNumpad er lige nu "+ skærmNumpad.getSize().width+","+ skærmNumpad.getSize().height+"\n og er placeret i ("+skærmNumpad.getLocation().x+", "+skærmNumpad.getLocation().y+")");
    }//GEN-LAST:event_formComponentShown

    private void antalTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_antalTextFocusLost
        if(antalText.getText().isEmpty()){
            antalText.setText("1");
        }
    }//GEN-LAST:event_antalTextFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField antalText;
    private javax.swing.JButton børneIsKnap;
    private javax.swing.JButton desserterKnap;
    private javax.swing.JButton isPindeOgVaflerKnap;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel kategoriCardLayoutPanel;
    private javax.swing.JButton limonadeIsKnap;
    private javax.swing.JButton literIsKnap;
    private javax.swing.JButton mixIsPakkerKnap;
    private javax.swing.JButton specialIsKnap;
    private javax.swing.JButton tooMuchKnap;
    // End of variables declaration//GEN-END:variables

}
